<?php
/**
 * 备货申请日志
 * User: bgl
 * Date: 2023/2/23
 * Time: 14:45
 */

namespace App\Admin\Controllers\ProductStockup;

use App\Models\Basics\SysArea;
use App\Models\Product\ProductClassify;
use App\Models\ProductStockup\StockupApplyLog;
use App\Util\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;

class StockupApplyLogController extends AdminController{

    protected $title = '备货日志（新）';

    protected function grid(){

        \Encore\Admin\Admin::style($this->_style());
        $grid = new Grid(new StockupApplyLog());
        $grid->model()->orderBy('id', 'DESC');
        $grid->column('skuClassify.title','品类')->totalRow('汇总');
        $grid->column('area.area','站点');
        $grid->column('sku.psku_code','SKU');
        $grid->column('skuClassify.season','季节性')->using(ProductClassify::$_season);
        $grid->column('sku.psku_cn','产品名称');
        $grid->column('ratio','销售比例')->display(function ($value){
            return ($value && $value > 0) ? $value * 100 . '%' : 0;
        });
        $grid->column('fba_instant_num','FBA在库')->totalRow();
        $grid->column('fba_transit_num','FBA在途')->totalRow();
        $grid->column('cn_instant_num','国内在库')->totalRow();
        $grid->column('cn_transit_num','国内在途')->totalRow();
        $grid->column('os_instant_num','海外仓在库')->totalRow();
        $grid->column('os_transit_num','海外仓在途')->totalRow();
        $grid->column('apply_num','备货数量')->totalRow();
        $grid->column('apply_remark','申请备注');
        $grid->column('apply_time','申请时间');
        $grid->column('apply_admin_id','备货人员')->display(function ($value){
            return User::getUsernameById($value);
        });
        $grid->column('approval_num','审批数量')->totalRow();
        $grid->column('approval_remark','审批备注');
        $grid->column('approval_time','审批时间');
        $grid->column('approval_admin_id','审批人员')->display(function ($value){
            return User::getUsernameById($value);
        });
        $grid->column('confirm_num','确认数量')->totalRow();
        $grid->column('confirm_remark','确认备注');
        $grid->column('confirm_time','确认时间');
        $grid->column('confirm_admin_id','确认人员')->display(function ($value){
            return User::getUsernameById($value);
        });
        $grid->column('log_status','类型')->display(function ($value){
            return StockupApplyLog::$LOG_STATUS[$value] ?? '';
        });
        $grid->column('log_message','操作信息');
        //$grid->disableExport();

        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->paginate(10);
        $grid->filter(function($filter){
            $filter->column(1/2, function ($filter) {
                $filter->where(function ($query) {
                    $query->whereHas('sku', function ($query) {
                        $query->where('psku_code', 'like', "%{$this->input}%")
                            ->orWhere('psku_cn', 'like', "%{$this->input}%");
                    });
                }, 'SKU','sku8')->placeholder('模糊查询SKU，名称');
                $filter->equal('area_id','站点')->select(SysArea::options());
            });
            $filter->column(1/2, function ($filter){
                $filter->equal('skuClassify.season', '季节性')->select(ProductClassify::$_season);
                $filter->equal('sku.classify_id', '品类')->select(ProductClassify::options());
            });
        });

        return $grid;
    }

    protected function _style(){
        return <<<STYLE
.table > thead > tr > th, .table > tbody > tr > td {
    white-space: nowrap;
}
.table-responsive {
    max-height: calc(80vh);
}
STYLE;
    }

}
